package com.sscaipiao.android.activity.Base;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.*;
import com.sscaipiao.android.R;
import com.sscaipiao.android.activity.DaLeTou.DaLeTouActivity;
import com.sscaipiao.android.activity.HallActivity;
import com.sscaipiao.android.activity.StartTogetherActivity;
import com.sscaipiao.android.common.Constants;
import com.sscaipiao.android.model.User;
import com.sscaipiao.android.utils.CaipiaoServiceHelper;
import com.sscaipiao.android.utils.StringUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static android.view.View.OnClickListener;

/**
 * Created by ppona_000 on 13-12-30.
 */
public class NormalPayActivity extends BaseActivity implements OnClickListener, AdapterView.OnItemClickListener {
	protected String date;
	protected String leftTime;
	protected List<String> history;
	protected List<String> choosen;
	protected List<Map<String, Object>> mList;
	protected int chooseNumber = 0;
	protected String mDoFunction;

	private TextView mTitleText;
	private Button mTitleBack;

	private TextView pay_add_hand;
	private TextView pay_add_auto;
	private EditText pay_add_number;
	private EditText pay_add_date;
	private CheckBox pay_agree;

	private TextView pay_together;
	private TextView pay_total;
	private TextView pay_sure;

	private TextView protocol;

	private ListView mListView;


	private onClickCallback mCallback;
	private int mMaxBall;
	private int mRandomBall;


	public interface onClickCallback {
		void onHistoryListClick(int index);
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
		setContentView(R.layout.normal_pay_activity);
		setupViews();
		choosen = user.getChoosen();
		saveDateParameter();
	
	}

	private void saveDateParameter() {
		Intent intent = getIntent();
		if (intent == null) {
			return;
		}
		String dataValue = intent.getStringExtra("date");
		date = dataValue;

	}

	private void setupViews() {
		mTitleText = (TextView) findViewById(R.id.title_text);
		mTitleBack = (Button) findViewById(R.id.title_back);
		mTitleBack.setOnClickListener(this);
		mListView = (ListView) findViewById(R.id.pay_listView);
		mListView.setOnItemClickListener(this);

		pay_add_hand = (TextView) findViewById(R.id.add_by_hand);
		pay_add_auto = (TextView) findViewById(R.id.add_by_auto);
		pay_add_number = (EditText) findViewById(R.id.pay_add_number);
		pay_add_date = (EditText) findViewById(R.id.pay_add_date);
		pay_agree = (CheckBox) findViewById(R.id.pay_agree);
		pay_together = (TextView) findViewById(R.id.pay_together);
		pay_total = (TextView) findViewById(R.id.pay_total);
		pay_sure = (TextView) findViewById(R.id.pay_sure);
		protocol = (TextView) findViewById(R.id.pay_regist_txt_protocol);
		pay_add_hand.setOnClickListener(this);
		pay_add_auto.setOnClickListener(this);
		pay_sure.setOnClickListener(this);
		pay_together.setOnClickListener(this);
		protocol.setOnClickListener(this);
		setupTextChangeListener();

	}

	public void setHistoryListClickCallback(onClickCallback cb) {
		mCallback = cb;
	}
	public void setRandom(int max, int random) {
		mMaxBall = max;
		mRandomBall = random;
	}

	public void setListAdapter(SimpleAdapter mAdapter) {
		mListView.setAdapter(mAdapter);

	}

	private void setupTextChangeListener() {
		pay_add_number.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
			}
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				// TODO Auto-generated method stub
			}
			@Override
			public void afterTextChanged(Editable s) {
				String sAgeFormat1 = getResources().getString(R.string.double_color_ball_add_bottom2);
				if (StringUtil.isNotEmptyString(pay_add_number.getText().toString()) &&
						StringUtil.isNotEmptyString(pay_add_date.getText().toString())) {
					pay_total.setText(String.format(sAgeFormat1, chooseNumber + "",
							Integer.parseInt(pay_add_number.getText().toString()),
							Integer.parseInt(pay_add_date.getText().toString()),
							totalMoney()));
				}
			}
		});
		pay_add_date.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
			}
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				String sAgeFormat1 = getResources().getString(R.string.double_color_ball_add_bottom2);
				if (StringUtil.isNotEmptyString(pay_add_number.getText().toString()) &&
						StringUtil.isNotEmptyString(pay_add_date.getText().toString())) {
					pay_total.setText(String.format(sAgeFormat1, chooseNumber + "",
							Integer.parseInt(pay_add_number.getText().toString()),
							Integer.parseInt(pay_add_date.getText().toString()),
							totalMoney()));
				}
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		if (mCallback != null) {
			mCallback.onHistoryListClick(arg2);
		}
	}

	public int totalMoney() {
		return 2 * chooseNumber * Integer.parseInt(pay_add_number.getText().toString()) *
				Integer.parseInt(pay_add_date.getText().toString());
	}


	public void recalculatorTotal() {
		String sAgeFormat1 = getResources().getString(R.string.double_color_ball_add_bottom2);
		pay_total.setText(String.format(sAgeFormat1, chooseNumber + "",
				Integer.parseInt(pay_add_number.getText().toString()),
				Integer.parseInt(pay_add_date.getText().toString()),
				totalMoney()));
	}






	public void setHistoryListAdapter(SimpleAdapter mAdapter) {
		mListView.setAdapter(mAdapter);

	}


	@Override
	public void onResume() {
		super.onResume();
		recalculatorTotal();
	}

	public void setTitleText(String string) {
		if (mTitleText != null) {
			mTitleText.setText(string);
		}
	}

	public void setTitleText(int id) {
		if (mTitleText != null) {
			String title = getResources().getString(id);
			setTitleText(title);
		}
	}

	public void setCallback(onClickCallback cb) {
		mCallback = cb;
	}

	@Override
	public void onClick(View v) {
		if (v == mTitleBack) {
			finish();
		}
		if (R.id.pay_regist_txt_protocol == v.getId()) {
			new AlertDialog.Builder(this).setTitle("用户投注协议")
					.setMessage(getResources().getString(R.string.user_protocol))
					.setPositiveButton("确定", null)
					.show();

		}

		if (R.id.pay_together  == v.getId() ){
			if(Integer.parseInt(getPayAddDate()) > 1) {
				Toast.makeText(this, "追号不可以发合买", Toast.LENGTH_SHORT).show();
				return;
			} else {
				Intent intent = new Intent( this,StartTogetherActivity.class);
				Bundle b = new Bundle();
				b.putSerializable(Constants.USER, user);
				b.putSerializable("date", date);
				b.putSerializable("totalMoney", totalMoney());
				b.putSerializable("schemeNum1", CaipiaoServiceHelper.combineSchemeNum1(user,getPayAddNumber(), totalMoney()+""));
				intent.putExtras(b);
				startActivity(intent);
			}
		}
	}

	public boolean doPayPreCheck() {
		if (chooseNumber <= 0) {
			Toast.makeText(this, "请投注", Toast.LENGTH_SHORT).show();
			return false;
		} else if (StringUtil.isEmptyString(pay_add_date.getText().toString()) ||
				Integer.parseInt(pay_add_date.getText().toString()) < 1 ||
				Integer.parseInt(pay_add_date.getText().toString()) > 14) {
			Toast.makeText(this, "请选择正数期数(1 - 14)", Toast.LENGTH_SHORT).show();
			return false;
		} else if (StringUtil.isEmptyString(pay_add_number.getText().toString()) ||
				Integer.parseInt(pay_add_number.getText().toString()) < 1) {
			Toast.makeText(this, "请选择正数倍数", Toast.LENGTH_SHORT).show();
			return false;
		} else if (!pay_agree.isChecked()) {
			Toast.makeText(this, "请阅读并确认 电话购彩协议", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}


	public void callMainAcitvity(Class activity) {
		Intent intent = new Intent(this, activity);
		Bundle b = new Bundle();
		b.putSerializable(Constants.USER, user);
		b.putSerializable("date", date);
		b.putSerializable("leftTime", leftTime);
		intent.putExtras(b);
		startActivity(intent);
	}
	public String  getPayAddNumber() {
		return  pay_add_number.getText().toString();
	}
	public String  getPayAddDate() {
		return  pay_add_date.getText().toString();
	}



}